package com.zp.system.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.DictVsClinic;
import com.zp.system.entity.bo.DictVsClinicBo;
import com.zp.system.entity.vo.DictVsClinicVo;
import com.zp.system.mapper.DictVsClinicMapper;
import com.zp.system.service.IDictVsClinicService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 字典与诊疗项目对照Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-03-09
 */
@RequiredArgsConstructor
@Service
public class DictVsClinicServiceImpl implements IDictVsClinicService {

    private final DictVsClinicMapper baseMapper;

    /**
     * 查询字典与诊疗项目对照
     */
    @Override
    public DictVsClinicVo queryById(String baseDictId) {
        return baseMapper.selectVoById(baseDictId);
    }

    /**
     * 查询字典与诊疗项目对照列表(分页)
     */
    @Override
    public TableDataInfo<DictVsClinicVo> queryPageList(DictVsClinicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DictVsClinic> lqw = buildQueryWrapper(bo);
        Page<DictVsClinicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询字典与诊疗项目对照列表(不分页)
     */
    @Override
    public List<DictVsClinicVo> queryList(DictVsClinicBo bo) {
        LambdaQueryWrapper<DictVsClinic> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DictVsClinic> buildQueryWrapper(DictVsClinicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DictVsClinic> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseDictId()), DictVsClinic::getBaseDictId, bo.getBaseDictId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicItemId()), DictVsClinic::getClinicItemId, bo.getClinicItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getImplementDept()), DictVsClinic::getImplementDept, bo.getImplementDept());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DictVsClinic::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutpOrInp()), DictVsClinic::getOutpOrInp, bo.getOutpOrInp());
        return lqw;
    }

    /**
     * 新增字典与诊疗项目对照
     */
    @Override
    public Boolean insertByBo(DictVsClinicBo bo) {
        DictVsClinic add = BeanUtil.toBean(bo, DictVsClinic.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBaseDictId(add.getBaseDictId());
        }
        return flag;
    }

    /**
     * 修改字典与诊疗项目对照
     */
    @Override
    public Boolean updateByBo(DictVsClinicBo bo) {
        DictVsClinic update = BeanUtil.toBean(bo, DictVsClinic.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DictVsClinic entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除字典与诊疗项目对照
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
